package thread.forkjoinpool;

import java.util.concurrent.RecursiveAction;

public class MyRecursiveAction extends RecursiveAction {

    private static final int THRESHOLD = 50;

    private final int start;

    private final int end;

    public MyRecursiveAction(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected void compute() {
        // 5000, 0, 50
        if (end - start < THRESHOLD) {
            // 重复的数据取在首部的
            for (int i = start; i < end; i++) {
                System.out.println("线程:" + Thread.currentThread().getName() + " i的值为:" + i + " time:" + System.currentTimeMillis());
            }
        } else {
            int middle = (start + end) / 2;
            System.out.println("middle:" + middle);
            MyRecursiveAction left = new MyRecursiveAction(start, middle);
            MyRecursiveAction right = new MyRecursiveAction(middle, end);
            left.fork();
            right.fork();
        }
    }
}
